#include <bits/stdc++.h>

using namespace std;
#define int long long

void solve()
{
	int n;
	cin >> n;
	vector<int> p(n), s(n), b(n);
	for (int i = 0; i < n; i++) cin >> p[i];
	for (int i = 0; i < n; i++) cin >> s[i];
	for (int i = 0; i < n; i++) b[i] = lcm(s[i], p[i]);
	int g = 0;
	for (int i = 0; i < n; i++)
	{
		g = __gcd(g, b[i]);
		if (g != p[i])
		{
			cout << "No" << '\n';
			return;
		}
	}
	g = 0;
	for (int i = n - 1; i >= 0; i--)
	{
		g = __gcd(g, b[i]);
		if (g != s[i])
		{
			cout << "No" << '\n';
			return;
		}
	}
	cout << "Yes" << '\n';
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int _ = 1;
	cin >> _;
	while (_--)
	{
		solve();
	}
	return 0;
}